/**
 * @author scylla
 * @version V1.0
 * @Project echo-java-base
 * @Title 线程方法
 * @Description *
 * @Package concurrent.mthread.self
 * @Since 2021/7/29 1:27 下午
 */
package concurrent.mthread.self;

import java.util.concurrent.TimeUnit;

public class ThreadMethod {

    private int countDown = 5;
    private Thread t;
    private String name;

    public ThreadMethod(String name) {
        this.name = name;
    }

    /**
     * 运行任务
     *
     * @author chentl
     * @since 1:41 下午 2021/7/29
     **/
    public void runTask() {
        if (t == null) {
            t = new Thread(name) {
                @Override
                public void run() {
                    try {
                        for (; ; ) {
                            System.out.println(this);
                            if (--countDown == 0) return;

                            TimeUnit.MILLISECONDS.sleep(10);
                        }
                    } catch (InterruptedException e) {
                        System.err.println(e.getMessage());
                    }
                }

                @Override
                public String toString() {
                    return getName() + ": " + countDown;
                }
            };

            t.start();
        }

    }

}
